#pragma once

#include"TcpServer.hpp"
#include"Protocol.hpp"
#include"PthreadPool.h"
#include"Task.hpp"
#include<signal.h>

class HttpServer {
  private:
    int _port;
    bool _stop;
  public:
    HttpServer(int port) :_port(port), _stop(false) {

    }
    void InitHttpServer() {
         signal(SIGPIPE, SIG_IGN); /*捕捉信号,防止读端关闭导致服务器写回时崩溃*/
         LOG(INFO, "InitHttpServer success");
    }
    void Loop() {
       /*获得线程池单例进行处理*/
        sockaddr_in endPoint;
        InitHttpServer();
        LOG(INFO, "Loop begin...");
        while (!_stop) {
            socklen_t len= sizeof(endPoint);
            int sock = accept( TcpServer::Getinstance(_port)-> GetListenSock(), (struct sockaddr*)&endPoint, &len);   
            if (sock < 0) {
                LOG(ERROR, "accept failure");
                continue;
            }
            LOG(INFO, "accept new link success..."); 
            Task t(sock); 
            PthreadPool::GetInstance() -> Push(t);
        }
    }
    ~HttpServer() {

    }
};
